using System;
using System.Collections.Generic;
using System.Text;

using Csla;
using Csla.Data;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;


namespace buildingLib.Prepare
{
    [Serializable()]
    public class AreaWhole : BusinessBase<AreaWhole>
    {
        #region Business Methods
        Guid m_id;
        string m_project = string.Empty;
        string m_zongdihao = string.Empty;
        string m_location = string.Empty;
        string m_tufu = string.Empty;
        double m_zongdiArea;
        double m_buildingArea;
        double m_upArea;
        double m_belowArea;
        double m_accArea;
        double m_renfangArea;
        int m_upFloors;
        int m_belowFloors;
        string m_memo = string.Empty;

        public Guid ID
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_id;
            }
        }

        public string Project
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_project;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_project != value)
                {
                    m_project = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Zongdihao
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_zongdihao;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_zongdihao != value)
                {
                    m_zongdihao = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Location
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_location;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_location != value)
                {
                    m_location = value;
                    PropertyHasChanged();
                }
            }
        }
        public string Tufu
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_tufu;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_tufu != value)
                {
                    m_tufu = value;
                    PropertyHasChanged();
                }
            }
        }
        public double ZongdiArea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_zongdiArea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_zongdiArea != value)
                {
                    m_zongdiArea = value;
                    PropertyHasChanged();
                }
            }
        }
        public double BuildingArea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_buildingArea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_buildingArea != value)
                {
                    m_buildingArea = value;
                    PropertyHasChanged();
                }
            }
        }
        public double UpArea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_upArea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_upArea != value)
                {
                    m_upArea = value;
                    PropertyHasChanged();
                }
            }
        }
        public double BelowArea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_belowArea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_belowArea != value)
                {
                    m_belowArea = value;
                    PropertyHasChanged();
                }
            }
        }
        public double AccArea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_accArea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_accArea != value)
                {
                    m_accArea = value;
                    PropertyHasChanged();
                }
            }
        }
        public double RenfangArea
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_renfangArea;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_renfangArea != value)
                {
                    m_renfangArea = value;
                    PropertyHasChanged();
                }
            }
        }
        public int UpFloors
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_upFloors;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_upFloors != value)
                {
                    m_upFloors = value;
                    PropertyHasChanged();
                }
            }
        }
        public int BelowFloors
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_belowFloors;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (m_belowFloors != value)
                {
                    m_belowFloors = value;
                    PropertyHasChanged();
                }
            }
        }
        
        public string Memo
        {
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            get
            {
                CanReadProperty(true);
                return m_memo;
            }
            [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]
            set
            {
                CanWriteProperty(true);
                if (value == null) value = string.Empty;
                if (m_memo != value)
                {
                    m_memo = value;
                    PropertyHasChanged();
                }
            }
        }

        protected override object GetIdValue()
        {
            return m_id;
        }
        #endregion

        #region Validation Rules

        protected override void AddBusinessRules()
        {
            // TODO: add validation rules
            //ValidationRules.AddRule(null, "");
        }

        #endregion

        #region Authorization Rules

        protected override void AddAuthorizationRules()
        {
            // TODO: add authorization rules
            //AuthorizationRules.AllowWrite("", "");
        }

        #endregion

        #region Factory Methods

        public static AreaWhole NewAreaWhole()
        {
            // TODO: change to use new keyword if not loading defaults
            return DataPortal.Create<AreaWhole>();
        }

        public static AreaWhole GetAreaWhole(Guid id)
        {
            return DataPortal.Fetch<AreaWhole>(new Criteria(id));
        }

        public static void DeleteAreaWhole(Guid id)
        {
            DataPortal.Delete(new Criteria(id));
        }

        private AreaWhole()
        {
            
        }
        #endregion

        #region Data Access
        [Serializable()]
        private class Criteria
        {
            private Guid _id;
            public Guid Id
            {
                get { return _id; }
            }

            public Criteria(Guid id)
            { _id = id; }
        }

        [RunLocal()]
        protected override void DataPortal_Create()
        {
            m_id = Guid.NewGuid();
            
            ValidationRules.CheckRules();
        }

        private void DataPortal_Fetch(Criteria criteria)
        {
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AGetAreaWhole");
            db.AddInParameter(dbcom, "id", DbType.Guid, criteria.Id);

            using (SafeDataReader dr = new SafeDataReader(db.ExecuteReader(dbcom)))
            {
                if (dr.Read())
                {
                    m_id = dr.GetGuid("id");

                    m_project = dr.GetString("project");
                    m_zongdihao = dr.GetString("zongdihao");
                    m_location = dr.GetString("location");
                    m_tufu = dr.GetString("tufu");
                    m_zongdiArea = dr.GetDouble("zongdiArea");
                    m_buildingArea = dr.GetDouble("buildingArea");
                    m_upArea = dr.GetDouble("upArea");
                    m_belowArea = dr.GetDouble("belowArea");
                    m_accArea = dr.GetDouble("accArea");
                    m_renfangArea = dr.GetDouble("renfangArea");
                    m_upFloors = dr.GetInt32("upFloors");
                    m_belowFloors = dr.GetInt32("belowFloors");
                    m_memo = dr.GetString("memo");
                }
            }
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AInsertAreaWhole");
            
            DoInsertUpdate(dbcom);
 
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            if (!this.IsDirty) return;
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_AUpdateAreaWhole");
            
            DoInsertUpdate(dbcom);
            MarkOld();
        }

        private void DoInsertUpdate(DbCommand dbcom)
        {
            Database db = SQLDatabase.GetDatabase();
            db.AddInParameter(dbcom, "id", DbType.Guid, m_id);
            db.AddInParameter(dbcom, "project", DbType.String, m_project);
            db.AddInParameter(dbcom, "zongdihao", DbType.String, m_zongdihao);
            db.AddInParameter(dbcom, "location", DbType.String, m_location);
            db.AddInParameter(dbcom, "tufu", DbType.String, m_tufu);
            db.AddInParameter(dbcom, "zongdiArea", DbType.Single, m_zongdiArea);
            db.AddInParameter(dbcom, "buildingArea", DbType.Single, m_buildingArea);
            db.AddInParameter(dbcom, "upArea", DbType.Single, m_upArea);
            db.AddInParameter(dbcom, "belowArea", DbType.Single, m_belowArea);
            db.AddInParameter(dbcom, "accArea", DbType.Single, m_accArea);
            db.AddInParameter(dbcom, "renfangArea", DbType.Single, m_renfangArea);
            db.AddInParameter(dbcom, "upFloors", DbType.Int32, m_upFloors);
            db.AddInParameter(dbcom, "belowFloors", DbType.Int32, m_belowFloors);
            db.AddInParameter(dbcom, "memo", DbType.String, m_memo);

            db.ExecuteNonQuery(dbcom);
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new Criteria(m_id));
        }

        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Delete(Criteria criteria)
        {
            Database db = SQLDatabase.GetDatabase();
            DbCommand dbcom = db.GetStoredProcCommand("usp_ADeleteAreaWhole");
            db.AddInParameter(dbcom, "id", DbType.Guid, criteria.Id);

            db.ExecuteNonQuery(dbcom);
        }
        #endregion
    }//end of class	
}//end of namespace 

